package luckPet

import (
	"github.com/gin-gonic/gin"
	"my-systems/pet-system/internal/service"
	"my-systems/pet-system/internal/sql/pet_sql"
	"my-systems/pet-system/pkg/e"
	"my-systems/pet-system/pkg/tools"
	"net/http"
)

func GetSalePet(ctx *gin.Context) {
	petSaleTmp := service.PetsAreAvailableForSale{}
	petID := ctx.Param("id")
	res := petSaleTmp.GetSalePetByID(petID)
	ctx.JSON(http.StatusOK, res)
}

func GetSalePets(ctx *gin.Context) {
	petSaleTmp := &service.PetsAreAvailableForSale{}
	err := ctx.ShouldBind(petSaleTmp)
	if err != nil {
		tools.Logger.Error(err.Error())
		ctx.JSON(400, gin.H{"error": err.Error()})
		return
	}
	res := petSaleTmp.GetSalePets(petSaleTmp.IsAvailable)
	ctx.JSON(http.StatusOK, res)
}

// AddPet 添加宠物
func AddPet(ctx *gin.Context) {
	petTmp := &service.Pet{}
	err := ctx.ShouldBind(petTmp)
	var res interface{}
	if err != nil {
		tools.Logger.Error(err.Error())
		ctx.JSON(400, gin.H{"error": err.Error()})
		return
	}
	// 获取用户token
	tokenString := ctx.GetHeader("Authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}

	// 1.获取头像文件
	file, fileHeader, err := ctx.Request.FormFile("avatar")
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"提示": "未上传头像文件，则使用默认封面",
		})
		res = petTmp.AddPets(claims.ID, nil)
	} else {
		defer file.Close()
		// 2.检查文件头
		if fileHeader == nil {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"error": "文件头为空",
			})
			return
		}
		res = petTmp.AddPets(claims.ID, file)

	}

	ctx.JSON(http.StatusOK, res)
}

func UpdatePet(ctx *gin.Context) {
	petTmp := &service.Pet{}
	err := ctx.ShouldBind(petTmp)
	if err != nil {
		tools.Logger.Error(err.Error())
		ctx.JSON(400, gin.H{"error": err.Error()})
		return
	}
	// 获取用户token
	tokenString := ctx.GetHeader("Authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析 Token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}
	res := petTmp.UpdatePet(claims.ID, petTmp.ID, petTmp)
	ctx.JSON(http.StatusOK, res)

}

func DeletePet(ctx *gin.Context) {
	petTmp := &service.Pet{}
	err := ctx.ShouldBind(petTmp)
	if err != nil {
		tools.Logger.Error(err.Error())
		ctx.JSON(400, gin.H{"error": err.Error()})
		return
	}
	// 获取用户token
	tokenString := ctx.GetHeader("Authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析 Token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}
	res := petTmp.DeletePet(petTmp.ID, claims.ID)
	ctx.JSON(http.StatusOK, res)
}

func UpPetAvatar(ctx *gin.Context) {
	// 获取用户token
	tokenString := ctx.GetHeader("Authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析 Token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}

	// 检查用户是否有宠物
	_, err = pet_sql.GetALlUserPet(claims.ID)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "该用户疑似没有宠物，无法给宠物上传图像",
		})
		return
	}

	petTmp := service.Pet{}
	err = ctx.ShouldBind(&petTmp)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"error": "参数绑定失败",
		})
		return
	}

	// 获取头像文件
	file, fileHeader, err := ctx.Request.FormFile("pet-avatar")
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"error": "未上传图像文件",
		})
		return
	}
	defer file.Close()
	// 2.检查文件头
	if fileHeader == nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"error": "文件头为空",
		})
		return
	}

	// 有宠物则根据宠物ID上传图像
	res := petTmp.UpPetAvatar(claims.ID, file)
	ctx.JSON(http.StatusOK, res)
}

func CatPet(ctx *gin.Context) {
	petTmp := &service.Pet{}
	// 获取用户token
	tokenString := ctx.GetHeader("Authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析 Token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}
	res := petTmp.GetALlUserPet(claims.ID)
	ctx.JSON(http.StatusOK, res)
}
